package com.niyue.sandbox.uclock.clockr;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class ImagePanel extends JPanel {
	private static final long serialVersionUID = -5261828085632486743L;

	private BufferedImage image;

	public BufferedImage getImage() {
		return image;
	}

	public void setImage(BufferedImage image) {
		this.image = null;
		this.image = image;
		Dimension size = new Dimension(image.getWidth(), image.getHeight());
	    setPreferredSize(size);
	    setMinimumSize(size);
	    setMaximumSize(size);
	    setSize(size);
	    setLayout(null);
		repaint();
	}

	public ImagePanel(BufferedImage image) {
		this.image = image;
	}

	public ImagePanel() {
		this.setBackground(Color.white);
	}

	public void paintComponent(Graphics g) {
		System.out.println("painting...");
		update(g);
	}

	public void update(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(this.getBackground());
		g2.fillRect(0, 0, this.getWidth(), this.getHeight());
		if (image != null){
			g2.drawImage(image, 0, 0, this);
			System.out.println("drawing image...");
		}			
		else
			System.out.println(image == null);
	}
}
